package facade

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	command_service "mine-info-server/app/command_service"
	"mine-info-server/app/query_service"
	"mine-info-server/global"
	"mine-info-server/infra/database/mysql/common-context/blessing-word/repo"
	repo2 "mine-info-server/infra/database/mysql/common-context/message/repo"
	"mine-info-server/interfaces/assembler"
	"mine-info-server/interfaces/request"
	"mine-info-server/utils/response"
)

/*
   @author:zhongyang
   @date:2023/4/11
   @description:通用上下文，存放通用性较强或未进行领域划分的业务
*/

type CommonServer struct {
	queryService   *query_service.CommonQueryService
	commandService *command_service.CommonCommandService
	assembler      assembler.CommonAssembler
}

func NewCommonServer() *CommonServer {
	return &CommonServer{
		queryService: &query_service.CommonQueryService{
			MsgRepo:      repo2.NewMessageRepo(),
			BlessingRepo: repo.NewBlessingWordRepo(),
		},
		commandService: &command_service.CommonCommandService{
			MsgRepo: repo2.NewMessageRepo(),
		},
	}
}

// Login 登陆
func (a *CommonServer) Login(c *gin.Context) {
	var req *request.LoginRequest
	_ = c.ShouldBindJSON(&req)
	command := a.assembler.ToCommandFromLoginRequest(req)
	if res, err := a.commandService.Login(c, command); err != nil {
		global.GVA_LOG.Error("CommonServer.Login 登录失败", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithData(gin.H{"token": res}, c)
	}
}

// GetUserInfo 获取用户信息
func (a *CommonServer) GetUserInfo(c *gin.Context) {
	if res, err := a.queryService.GetUserInfo(); err != nil {
		global.GVA_LOG.Error("查询失败", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithData(gin.H{"res": res}, c)
	}
}

// GetOnlineUserTotal 获取在线用户数
func (a *CommonServer) GetOnlineUserTotal(c *gin.Context) {
	if res, err := a.queryService.GetOnlineUserTotal(c); err != nil {
		global.GVA_LOG.Error("查询失败", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithData(gin.H{"total": res}, c)
	}
}

// IsMsgAllRead 是否全部已读
func (a *CommonServer) IsMsgAllRead(c *gin.Context) {
	if res, err := a.queryService.IsMsgAllRead(c); err != nil {
		global.GVA_LOG.Error("CommonServer.IsMsgAllRead 获取失败", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithData(gin.H{"is_all_read": res}, c)
	}
}

// GetMsgList 获取消息列表
func (a *CommonServer) GetMsgList(c *gin.Context) {
	var req *request.GetMsgListRequest
	err := c.ShouldBindQuery(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	query := a.assembler.ToQueryFromGetMsgListRequest(c, req)
	if res, total, err := a.queryService.GetMsgList(c, query); err != nil {
		global.GVA_LOG.Error("CommonServer.GetMsgList 获取失败", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     res,
			Total:    total,
			Page:     req.Page,
			PageSize: req.PageSize,
		}, "获取成功", c)
	}
}

// ReadMsg 读取消息
func (a *CommonServer) ReadMsg(c *gin.Context) {
	var req *request.GetById
	_ = c.ShouldBindJSON(&req)
	command := a.assembler.ToCommandFromReadMsgRequest(req)
	if err := a.commandService.ReadMsg(c, command); err != nil {
		global.GVA_LOG.Error("CommonServer.ReadMsg 修改失败", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.Ok(c)
	}
}

// GetBlessingWord 随机获取鸡汤文案
func (a *CommonServer) GetBlessingWord(c *gin.Context) {
	if res, err := a.queryService.GetBlessingWord(); err != nil {
		global.GVA_LOG.Error("获取失败", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithData(gin.H{"data": res}, c)
	}
}
